package com.company.aspect;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;

import org.springframework.util.ReflectionUtils;

import com.company.aspect.annotation.LogMethod;
import com.company.aspect.annotation.LogParam;
import com.company.aspect.annotation.OperationType;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Test {
    String info() default "";
}


public class Demo {
    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();    
        
        Method[] methods = demo.getClass().getMethods();
        
        for (Method method : methods) {        	
        	if(method.isAnnotationPresent(LogMethod.class)) {
        		System.out.println("Here");
        		
        		LogMethod logMethod = method.getAnnotation(LogMethod.class);
        		System.out.println(logMethod.type()); 
        		
        		Class[] paramTypes = method.getParameterTypes();
        		
        		System.out.println();
        	}
        }
        
//        Method[] methods = demo.getClass().getMethods();
//        
//        for (Method method : methods) {        	
//        	if(method.isAnnotationPresent(Test.class)) {
//        		final Class[] paramTypes = method.getParameterTypes();        		        		
//        		
//        		Annotation[][] paramAnnotations = method.getParameterAnnotations();
//        		
//        		for(int i=0; i < paramAnnotations.length; i++) {
//        			for (Annotation annotation : paramAnnotations[i]) {
//						if(annotation instanceof LogParam) {
//							System.out.println(String.format("parameter %d with type %s is annotated with @LogParam", i, paramTypes[i]));
//						}
//					}
//        		}
//                            	
//        	}        	        	           
//        }
//        
////        Method m = demo.getClass().getDeclaredMethod("a", String.class, int.class);
//        Method m = ReflectionUtils.findMethod(demo.getClass(), "a", String.class, int.class);
//        Object obj = m.invoke(demo, "1", 1);
//        System.out.println(obj);
    }
    
    @LogMethod(type=OperationType.SAVE)
    public String a(String a, @LogParam int b) {
    	System.out.println("called");
    	
    	return "hi";
    }
}